package tw.edu.tku.im.maze;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.os.Parcel;
import android.os.Parcelable;

public class MarkRep extends RepPacket {
   String timestamp;
   MazeCell[] marks;
   MazeCell[] unmarks;
   GameState gameState;

   public MarkRep(String timestamp, MazeCell[] marks, GameState gameState) {
      this(timestamp, marks, null, gameState);
   }
   
   public MarkRep(String timestamp, MazeCell[] marks, MazeCell[] unmarks, 
                  GameState gameState) 
   {
      this.timestamp = timestamp;
      this.marks = marks;
      this.unmarks = unmarks;
      this.gameState = gameState;
   }
   
   public String getTimestamp() {
      return timestamp;
   }

   public void setTimestamp(String timestamp) {
      this.timestamp = timestamp;
   }

   public MazeCell[] getMarks() {
      return marks;
   }

   public void setMarks(MazeCell[] marks) {
      this.marks = marks;
   }

   public MazeCell[] getUnmarks() {
      return unmarks;
   }

   public void setUnmarks(MazeCell[] unmarks) {
      this.unmarks = unmarks;
   }

   public GameState getGameState() {
      return gameState;
   }

   public void setGameState(GameState gameState) {
      this.gameState = gameState;
   }
   
   @Override
   public String toJSONString() throws JSONException {
      JSONObject jsonObj = new JSONObject();
      JSONObject result = new JSONObject();
      result.put("timestamp", this.timestamp);
      if(this.marks!=null) {
         JSONArray marks = new JSONArray();
         for(MazeCell cell : this.marks) {
            if(cell==null) { break; }
            marks.put(new JSONObject(cell.toJSONString()));
         }
         result.put("mark", marks);
      }
      if(this.unmarks!=null) {
         JSONArray unmarks = new JSONArray();
         for(MazeCell cell : this.unmarks) {
            if(cell==null) { break; }
            unmarks.put(new JSONObject(cell.toJSONString()));
         }
         result.put("unmark", unmarks);
      }
      result.put("gameState", new JSONObject(this.gameState.toJSONString()));
      jsonObj.put("result", result);
      jsonObj.put("action", "markResponse");
      return jsonObj.toString();
   }
   
   @Override
   public String getPacketName() {
      return "markResponse";
   }

   @Override
   public int describeContents() {
      return 0;
   }

   @Override
   public void writeToParcel(Parcel arg0, int arg1) {
      
   }

}
